import javax.swing.JPanel;
import javax.swing.SpringLayout;
import javax.swing.JTextField;
import javax.swing.JSeparator;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;
import java.awt.Color;
import javax.swing.SwingConstants;

/**
 * This panel is used to add users to our system.
 * @author Matt Lee (ONLY of this javadoc)
 *
 */
public class AddUserPanel extends JPanel{
	private JTextField txtName;
	
	private JTextField txtAddress;
	
	private JTextField txtContactPhone;
	
	private JTextField txtEmailAddress;
	
	private JTextField txtUsername;
	
	private JTextField txtPassword;
	
	private JLabel lblTheSystemAdministrator;
	
	private JButton btnCreateUser;
	
	JComboBox comboBox;
	
	private JLabel lblResult;

	private class buttonListen implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			User user = new User(txtName.getText(), txtUsername.getText(), txtPassword.getText(), 
					txtAddress.getText(), txtContactPhone.getText(), txtEmailAddress.getText(), null);
			int UserType;
			if(comboBox.getSelectedIndex() == 0) UserType = UserDatabase.PATIENT;
			else if(comboBox.getSelectedIndex() == 1) UserType = UserDatabase.NURSE;
			else if(comboBox.getSelectedIndex() == 2) UserType = UserDatabase.DOCTOR;
			else UserType = UserDatabase.SYSTEM_ADMIN;
			
			String result = UserDatabase.addUser(user, UserType);
			lblResult.setText(result);
			DataSaveLoad.save();
			DataSaveLoad.load();
		}
		
	}
	
	public AddUserPanel() {
		SpringLayout springLayout = new SpringLayout();
		setLayout(springLayout);
		
		txtName = new JTextField();
		springLayout.putConstraint(SpringLayout.WEST, txtName, 85, SpringLayout.WEST, this);
		add(txtName);
		txtName.setColumns(10);
		
		txtAddress = new JTextField();
		springLayout.putConstraint(SpringLayout.SOUTH, txtName, -18, SpringLayout.NORTH, txtAddress);
		springLayout.putConstraint(SpringLayout.NORTH, txtAddress, 153, SpringLayout.NORTH, this);
		springLayout.putConstraint(SpringLayout.WEST, txtAddress, 85, SpringLayout.WEST, this);
		add(txtAddress);
		txtAddress.setColumns(10);
		
		txtContactPhone = new JTextField();
		springLayout.putConstraint(SpringLayout.NORTH, 
				txtContactPhone, 19, SpringLayout.SOUTH, txtAddress);
		springLayout.putConstraint(SpringLayout.WEST, txtContactPhone, 85, SpringLayout.WEST, this);
		add(txtContactPhone);
		txtContactPhone.setColumns(10);
		
		txtEmailAddress = new JTextField();
		springLayout.putConstraint(SpringLayout.NORTH, 
				txtEmailAddress, 18, SpringLayout.SOUTH, txtContactPhone);
		springLayout.putConstraint(SpringLayout.WEST, txtEmailAddress, 85, SpringLayout.WEST, this);
		add(txtEmailAddress);
		txtEmailAddress.setColumns(10);
		
		txtUsername = new JTextField();
		springLayout.putConstraint(SpringLayout.NORTH, txtUsername, 0, SpringLayout.NORTH, txtAddress);
		add(txtUsername);
		txtUsername.setColumns(10);
		
		txtPassword = new JTextField();
		springLayout.putConstraint(SpringLayout.NORTH, txtPassword, 0, SpringLayout.NORTH, txtContactPhone);
		add(txtPassword);
		txtPassword.setColumns(10);
		
		btnCreateUser = new JButton("Create User");
		springLayout.putConstraint(SpringLayout.NORTH, btnCreateUser, 17, SpringLayout.SOUTH, txtPassword);
		springLayout.putConstraint(SpringLayout.WEST, btnCreateUser, 37, SpringLayout.EAST, txtEmailAddress);
		springLayout.putConstraint(SpringLayout.SOUTH, btnCreateUser, 40, SpringLayout.SOUTH, txtPassword);
		springLayout.putConstraint(SpringLayout.EAST, btnCreateUser, 201, SpringLayout.EAST, txtEmailAddress);
		add(btnCreateUser);
		btnCreateUser.addActionListener(new buttonListen());
		
		JLabel lblAddANew = new JLabel("Add a New User:");
		springLayout.putConstraint(SpringLayout.NORTH, 
				lblAddANew, 22, SpringLayout.NORTH, this);
		springLayout.putConstraint(SpringLayout.WEST, 
				lblAddANew, 149, SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.EAST, lblAddANew, 308, SpringLayout.WEST, this);
		lblAddANew.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 16));
		add(lblAddANew);
		
		lblTheSystemAdministrator = 
			new JLabel("System Administrator must " +
					"approve the Username and Password.");
		springLayout.putConstraint(SpringLayout.NORTH, txtName, 31, SpringLayout.SOUTH, lblTheSystemAdministrator);
		springLayout.putConstraint(SpringLayout.NORTH, lblTheSystemAdministrator, 26, SpringLayout.SOUTH, lblAddANew);
		springLayout.putConstraint(SpringLayout.SOUTH, lblTheSystemAdministrator, 42, SpringLayout.SOUTH, lblAddANew);
		springLayout.putConstraint(SpringLayout.EAST, lblTheSystemAdministrator, 0, SpringLayout.EAST, txtUsername);
		lblTheSystemAdministrator.setHorizontalAlignment(SwingConstants.CENTER);
		lblTheSystemAdministrator.setFont(new Font("Tahoma", Font.PLAIN, 9));
		add(lblTheSystemAdministrator);
		
		JLabel lblName = new JLabel("Name:");
		springLayout.putConstraint(SpringLayout.WEST, lblTheSystemAdministrator, 0, SpringLayout.WEST, lblName);
		springLayout.putConstraint(SpringLayout.NORTH, lblName, 118, SpringLayout.NORTH, this);
		lblName.setFont(new Font("Tahoma", Font.BOLD, 11));
		springLayout.putConstraint(SpringLayout.WEST, lblName, 27, SpringLayout.WEST, this);
		add(lblName);
		
		JLabel lblAddress = new JLabel("Address:");
		springLayout.putConstraint(SpringLayout.NORTH, lblAddress, 3, SpringLayout.NORTH, txtAddress);
		springLayout.putConstraint(SpringLayout.EAST, lblAddress, 0, SpringLayout.EAST, lblName);
		lblAddress.setFont(new Font("Tahoma", Font.BOLD, 11));
		add(lblAddress);
		
		JLabel lblPhone = new JLabel("Phone #:");
		springLayout.putConstraint(SpringLayout.NORTH, lblPhone, 3, SpringLayout.NORTH, txtContactPhone);
		springLayout.putConstraint(SpringLayout.WEST, lblPhone, 0, SpringLayout.WEST, lblAddress);
		lblPhone.setFont(new Font("Tahoma", Font.BOLD, 11));
		add(lblPhone);
		
		JLabel lblNewLabel = new JLabel("E-Mail:");
		springLayout.putConstraint(SpringLayout.NORTH, lblNewLabel, 3, SpringLayout.NORTH, txtEmailAddress);
		springLayout.putConstraint(SpringLayout.EAST, lblNewLabel, 0, SpringLayout.EAST, lblName);
		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
		add(lblNewLabel);
		
		JLabel lblUsername = new JLabel("Username:");
		springLayout.putConstraint(SpringLayout.EAST, txtUsername, 103, SpringLayout.EAST, lblUsername);
		springLayout.putConstraint(SpringLayout.WEST, lblUsername, 37, SpringLayout.EAST, txtAddress);
		springLayout.putConstraint(SpringLayout.WEST, txtUsername, 17, SpringLayout.EAST, lblUsername);
		springLayout.putConstraint(SpringLayout.NORTH, lblUsername, 3, SpringLayout.NORTH, txtAddress);
		lblUsername.setFont(new Font("Tahoma", Font.BOLD, 11));
		add(lblUsername);
		
		JLabel lblPassword = new JLabel("Password:");
		springLayout.putConstraint(SpringLayout.WEST, txtPassword, 17, SpringLayout.EAST, lblPassword);
		springLayout.putConstraint(SpringLayout.EAST, txtPassword, 103, SpringLayout.EAST, lblPassword);
		springLayout.putConstraint(SpringLayout.NORTH, lblPassword, 3, SpringLayout.NORTH, txtContactPhone);
		springLayout.putConstraint(SpringLayout.EAST, lblPassword, 0, SpringLayout.EAST, lblUsername);
		lblPassword.setFont(new Font("Tahoma", Font.BOLD, 11));
		add(lblPassword);
		
		JLabel lblUserType = new JLabel("User Type:");
		springLayout.putConstraint(SpringLayout.NORTH, lblUserType, 34, SpringLayout.SOUTH, lblTheSystemAdministrator);
		springLayout.putConstraint(SpringLayout.EAST, lblUserType, 0, SpringLayout.EAST, lblUsername);
		lblUserType.setFont(new Font("Tahoma", Font.BOLD, 11));
		add(lblUserType);
		
		String[] options = { "Patient", "Nurse", "Doctor", "Admin"};
		comboBox = new JComboBox(options);
		springLayout.putConstraint(SpringLayout.WEST, comboBox, 17, SpringLayout.EAST, lblUserType);
		springLayout.putConstraint(SpringLayout.SOUTH, comboBox, -18, SpringLayout.NORTH, txtUsername);
		springLayout.putConstraint(SpringLayout.EAST, comboBox, 103, SpringLayout.EAST, lblUserType);
		comboBox.setEditable(false);
		comboBox.setSelectedIndex(0);
		add(comboBox);
		
		lblResult = new JLabel("");
		springLayout.putConstraint(SpringLayout.WEST, lblResult, 149, SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.EAST, lblResult, -137, SpringLayout.EAST, this);
		lblResult.setHorizontalAlignment(SwingConstants.CENTER);
		springLayout.putConstraint(SpringLayout.SOUTH, lblResult, -10, SpringLayout.SOUTH, this);
		lblResult.setForeground(Color.RED);
		lblResult.setFont(new Font("Times New Roman", Font.ITALIC, 13));
		add(lblResult);
	}
	
	/**
	 * Gets the text for username.
	 * @return The TextField containing this.
	 */
	public JTextField getTxtUsername() {
		return txtUsername;
	}
	
	/**
	 * Gets the address corresponding to text.
	 * @return the TextField corresponding to this.
	 */
	public JTextField getTxtAddress() {
		return txtAddress;
	}
	
	/**
	 * Returns the button for createUser.
	 * @return the corresponding JButton.
	 */
	public JButton getBtnCreateUser() {
		return btnCreateUser;
	}
	
	/**
	 * Retruns the textField corresponding to contact phone.
	 * @return the textField corresponding to this.
	 */
	public JTextField getTxtContactPhone() {
		return txtContactPhone;
	}
	
	/**
	 * Retruns the textField corresponding to email address.
	 * @return the textField corresponding to this.
	 */
	public JTextField getTxtEmailAddress() {
		return txtEmailAddress;
	}
	
	/**
	 * Retruns the textField corresponding to password.
	 * @return the textField corresponding to this.
	 */
	public JTextField getTxtPassword() {
		return txtPassword;
	}
	
	/**
	 * Retruns a comboBox.
	 * @return the comboBox.
	 */
	public JComboBox getComboBox() {
		return comboBox;
	}
	
	/**
	 * Retruns the textField corresponding to the name.
	 * @return the textField corresponding to this.
	 */
	public JTextField getTxtName() {
		return txtName;
	}
	
	/**
	 * Retruns the label corresponding to the lab result.
	 * @return the JLabel.
	 */
	public JLabel getLblResult() {
		return lblResult;
	}
	
	public String toString(){
		return "AddUser";
	}
}
